package com.hospital.insurancepeople.async;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.common.core.constants.InsuranceFinal;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DataBaseValue;
import com.hospital.insurancepeople.config.DbContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * timetask
 *
 * @description 处理微信支付功能
 * @author:louislau
 * @create 2019-11-08 08:31:47
 **/
@Service
@Slf4j
public class WxPayFunc {

    @Async
    public void wxPay(String params, String url,String printLog,RestTemplate restTemplate){
        String resStr=null;
        try {

//            // 请求体
//            HashMap<String, String> hashMap = new HashMap<>();
//            hashMap.put("key", params);
//            String body = JSONObject.toJSONString(hashMap);
//
//            // 请求头
//            HttpHeaders headers = new HttpHeaders();
//            headers.add("Content-Type", "application/json; charset=utf-8");
//            headers.add("X-Api-Sign-Version", "2.0.0");
//            // 请求
//            HttpEntity<String> request = new HttpEntity<>(body, headers);

            // 使用RestTemplate请求
            JSONObject jsonObject = JSON.parseObject(params);
            jsonObject.put("hospitalCode", DataBaseValue.getDataBaseVaule(DbContextHolder.getDbType()));
            ResponseEntity<JSONObject> responseBody = restTemplate.postForEntity(url, jsonObject.toJSONString(), JSONObject.class);
            JSONObject httpBody = responseBody.getBody();
            resStr = httpBody.toJSONString();
            if("000000".equals(httpBody.getString("respCode"))){
                log.info(printLog+  "\r\n推送描述：推送人保执行成功\r\n返回JSON:" + resStr);
            }else{
                log.info(printLog+  "\r\n推送描述：推送人保执行失败\r\n返回JSON:" + resStr);
            }
        }catch (Exception e){
            log.error( printLog+  "\r\n推送描述：推送人保执行失败\r\n返回JSON:" + resStr +"\r\n异常信息："  +e.toString());
        }
    }


    /**
     * @param params:请求body
     * @param url:请求url
     * @param channel:渠道
     * @param fromApp:发起方
     * @param toApp:请求方
     * @description:加密及解密验http请求
     * @author: lvyao
     * @date: 2023/3/24 8:32
     * @return: com.hospital.insurmovepay.result.Tip
     **/
    public Tip enDescHttp(JSONObject params, String url, String channel, String fromApp, String toApp, String dataType, RestTemplate restTemplate) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(InsuranceFinal.CHANNEL, channel);
        jsonObject.put(InsuranceFinal.FROMAPP, fromApp);
        jsonObject.put(InsuranceFinal.TOAPP, toApp);
        jsonObject.put(InsuranceFinal.DATATYPE, dataType);
        jsonObject.put(InsuranceFinal.DATA, params);
        Tip tip = new SuccessTip();
        try {
            //直接调用医保时的入参设置及返回解析 start
//            //设置请求头
            HttpHeaders httpHeaders = new HttpHeaders();
            //传递请求体时必须设置传递参数的格式，为Content-Type ： application/json
            httpHeaders.add("Content-Type", "application/json;charset=UTF-8");
            // 2.请求头 & 请求体
            HttpEntity<JSON> fromEntity = new HttpEntity(jsonObject.toJSONString(), httpHeaders);
            ResponseEntity<String> response = restTemplate.postForEntity(url, fromEntity, String.class);
            String result = response.getBody();
            if (response.getStatusCodeValue() == 200) {
                JSONObject convertValue = JSON.parseObject(result);
                if (InsuranceFinal.SUCCESS.equals(convertValue.getString(InsuranceFinal.RES_CODE))
                        || "200".equals(convertValue.getString(InsuranceFinal.RES_CODE))) {
                    tip.setData(convertValue.get("data"));
                    return tip;
                } else {
                    tip = new ErrorTip("302", convertValue.toJSONString());
                }

            } else {
                tip = new ErrorTip("304", JSON.toJSONString(response));
            }
        } catch (Exception e) {
            tip = new ErrorTip("303", e.getMessage());
        }
        return tip;
    }

}
